(function(angular) {
    "use strict";

    var ctl_module = angular.module("cloapCertificateApp.controller");

    /* 统计查询 */
    ctl_module.controller("statisticsCtl", function($scope) {
        $scope.splitDataByPage = function(datas, page_info) {
            page_info.perpage_items =  datas.slice(
                page_info.perpage_count * (page_info.current_page - 1),
                page_info.perpage_count * page_info.current_page);
        };
    });

    /* 持证缺失分析 */
    ctl_module.controller("statistics.lost.listCtl", function($scope, Statistics) {
        $scope.losts = Statistics.query_lost();
        $scope.widget_title = "持证缺失分析";
    });

    /* 持证率分析 */
    ctl_module.controller("statistics.hold_rate.listCtl", function($scope, Statistics) {
        $scope.page_info = {
            widget_title: "持证率分析",
            perpage_count: 10
        };

        $scope.searchHoldRate = function() {
            Statistics.query_ratio({to: $scope.page_info.to}, function(response_rates) {
                $scope.filter_rates = $scope.hold_rates = response_rates;
                $scope.page_info.current_page = 1;
                $scope.pageChanged();
            });
        };

        $scope.searchHoldRate();

        $scope.actualHoldRates = function(should_hold_count, hold_count) {
            if (should_hold_count <= 0) {
              return 0;
            }
            return (hold_count / should_hold_count) * 100; 
        };

        $scope.pageChanged = function() {
            $scope.splitDataByPage($scope.filter_rates, $scope.page_info);
        };
    });

   /* 持证到期分析 */
   ctl_module.controller("statistics.duedate.listCtl", function($scope, Statistics) {
       $scope.page_info = {
           widget_title: "持证到期分析",
           perpage_duedates: Statistics.query_duedate()
       };

       $scope.searchDuedate = function() {
           $scope.page_info.perpage_duedates = Statistics.query_duedate({to: $scope.page_info.to});
       };
   });

}(angular));
